<template>
  <page-header-wrapper>
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="48">
            <a-col :md="8" :sm="24">
              <a-form-item label="设备编号">
                <a-input v-model="queryParam.droneId" placeholder=""/>
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="设备状态">
                <a-select v-model="queryParam.status" placeholder="请选择" default-value="0">
                  <a-select-option value="0">全部</a-select-option>
                  <a-select-option value="1">空闲</a-select-option>
                  <a-select-option value="2">任务中</a-select-option>
                  <a-select-option value="3">维护中</a-select-option>
                  <a-select-option value="4">故障</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="8" :sm="24">
                <a-form-item label="设备型号">
                  <a-input v-model="queryParam.model" placeholder="请输入设备型号"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="维护日期">
                  <a-date-picker v-model="queryParam.maintenanceDate" style="width: 100%" placeholder="请选择维护日期"/>
                </a-form-item>
              </a-col>
              <a-col :md="8" :sm="24">
                <a-form-item label="载重范围">
                  <a-input-number
                    v-model="queryParam.maxPayload"
                    style="width: 100%"
                    placeholder="最大载重(kg)"
                    :min="0"
                    :max="100"
                  />
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="!advanced && 8 || 24" :sm="24">
              <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => this.queryParam = {}">重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <div class="table-operator">
        <a-button type="primary" icon="plus" @click="handleAdd">新增设备</a-button>
        <a-dropdown v-action:edit v-if="selectedRowKeys.length > 0">
          <a-menu slot="overlay">
            <a-menu-item key="1"><a-icon type="tool" />批量维护</a-menu-item>
            <a-menu-item key="2"><a-icon type="delete" />批量删除</a-menu-item>
          </a-menu>
          <a-button style="margin-left: 8px">
            批量操作 <a-icon type="down" />
          </a-button>
        </a-dropdown>
      </div>

      <s-table
        ref="table"
        size="default"
        rowKey="key"
        :columns="columns"
        :data="loadData"
        :alert="true"
        :rowSelection="rowSelection"
        showPagination="auto"
      >
        <span slot="serial" slot-scope="text, record, index">
          {{ index + 1 }}
        </span>
        <span slot="status" slot-scope="text">
          <a-badge :status="text | statusTypeFilter" :text="text | statusFilter" />
        </span>
        <span slot="waterproof" slot-scope="text">
          <a-badge :status="text ? 'success' : 'default'" :text="text ? '是' : '否'" />
        </span>
        <span slot="location" slot-scope="text">
          <a-tooltip>
            <template slot="title">
              经度: {{ text[0] }}<br>
              纬度: {{ text[1] }}
            </template>
            <a>查看位置</a>
          </a-tooltip>
        </span>

        <span slot="action" slot-scope="text, record">
          <template>
            <a @click="handleEdit(record)">编辑</a>
            <a-divider type="vertical" />
            <a @click="handleMaintenance(record)">维护记录</a>
          </template>
        </span>
      </s-table>

      <create-form
        ref="createModal"
        :visible="visible"
        :loading="confirmLoading"
        :model="mdl"
        @cancel="handleCancel"
        @ok="handleOk"
      />
    </a-card>
  </page-header-wrapper>
</template>

<script>
import moment from 'moment'
import { STable } from '@/components'
import CreateForm from './modules/CreateForm'

const columns = [
  {
    title: '#',
    scopedSlots: { customRender: 'serial' }
  },
  {
    title: '设备编号',
    dataIndex: 'droneId'
  },
  {
    title: '设备型号',
    dataIndex: 'model'
  },
  {
    title: '设备状态',
    dataIndex: 'status',
    scopedSlots: { customRender: 'status' }
  },
  {
    title: '当前位置',
    dataIndex: 'location',
    scopedSlots: { customRender: 'location' }
  },
  {
    title: '最大载重(kg)',
    dataIndex: 'maxPayload',
    sorter: true
  },
  {
    title: '续航时间(min)',
    dataIndex: 'flightTime',
    sorter: true
  },
  {
    title: '防水',
    dataIndex: 'waterproof',
    scopedSlots: { customRender: 'waterproof' }
  },
  {
    title: '上次维护',
    dataIndex: 'lastMaintenance',
    sorter: true
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: '150px',
    scopedSlots: { customRender: 'action' }
  }
]

const statusMap = {
  1: {
    status: 'default',
    text: '空闲'
  },
  2: {
    status: 'processing',
    text: '任务中'
  },
  3: {
    status: 'warning',
    text: '维护中'
  },
  4: {
    status: 'error',
    text: '故障'
  }
}

export default {
  name: 'DroneList',
  components: {
    STable,
    CreateForm
  },
  data () {
    this.columns = columns
    return {
      visible: false,
      confirmLoading: false,
      mdl: null,
      advanced: false,
      queryParam: {},
      loadData: parameter => {
        // 这里模拟后端数据
        const data = {
          pageSize: parameter.pageSize,
          pageNo: parameter.pageNo,
          totalCount: 100,
          data: Array(10).fill().map((_, index) => ({
            key: index,
            droneId: `DRONE-${String(index + 1).padStart(3, '0')}`,
            model: ['DJI-M300', 'DJI-M30', 'AUTEL-EVO'][Math.floor(Math.random() * 3)],
            status: Math.floor(Math.random() * 4) + 1,
            location: [
              106.504962 + (Math.random() - 0.5) * 0.1,
              29.533155 + (Math.random() - 0.5) * 0.1
            ],
            maxPayload: Math.floor(Math.random() * 50) + 10,
            flightTime: Math.floor(Math.random() * 30) + 30,
            waterproof: Math.random() > 0.5,
            lastMaintenance: moment().subtract(Math.floor(Math.random() * 30), 'days').format('YYYY-MM-DD')
          }))
        }
        return new Promise(resolve => {
          resolve({
            pageSize: data.pageSize,
            pageNo: data.pageNo,
            totalCount: data.totalCount,
            data: data.data
          })
        })
      },
      selectedRowKeys: [],
      selectedRows: []
    }
  },
  filters: {
    statusFilter (type) {
      return statusMap[type].text
    },
    statusTypeFilter (type) {
      return statusMap[type].status
    }
  },
  computed: {
    rowSelection () {
      return {
        selectedRowKeys: this.selectedRowKeys,
        onChange: this.onSelectChange
      }
    }
  },
  methods: {
    handleAdd () {
      this.mdl = null
      this.visible = true
    },
    handleEdit (record) {
      this.visible = true
      this.mdl = { ...record }
    },
    handleMaintenance (record) {
      this.$message.info(`查看设备 ${record.droneId} 的维护记录`)
    },
    handleOk () {
      // ... 处理表单提交
    },
    handleCancel () {
      this.visible = false
      const form = this.$refs.createModal.form
      form.resetFields()
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    }
  }
}
</script>
